/**
 * Created by Administrator on 2017/4/12.
 */
"use strict";
swan_pc
    .controller("MyOppListController",function($scope,$rootScope,$cookieStore,$state,opportunityByEO,oppBase) {


        var _user=$cookieStore.get("user");
        console.log("USER",_user)
        $cookieStore.put('_currentOrganizationId',_user.data.organizationId)
        $rootScope.organizationId=$cookieStore.get('_currentOrganizationId');

        $scope.userId=_user.data.id;
        $scope.isMyOpp=true;

        $scope.getAllEOOpp=function () {
            $scope.showResponsible=false;
            opportunityByEO.query({page: 0, size: 1000, sort: 'asc',id:_user.data.id},function (result) {
                $scope.oppList=result;
                console.log("OPP LIST",$scope.oppList);
                $scope.initPageSort(result);
            })
        }

        $scope.getAllEOOpp();

        $scope.showResponse=function () {
            $scope.showResponsible=true;
            $scope.responseList=[];
            _.forEach($scope.oppList,function (item) {
                if (item.ownerId==$scope.userId){
                    $scope.responseList.push(item)
                }
            })
            $scope.initPageSort($scope.responseList);
        }

        $scope.selectOne=function (item) {
            $rootScope.opp=item;
            $state.go('myOppBase');
        }

        $scope.stateList=[
            {
                name:'策划',
                value:0,
                select:false,
            },
            {
                name:'准备',
                value:1,
                select:false,
            },
            {
                name:'招标',
                value:2,
                select:false,
            },
            {
                name:'评标',
                value:3,
                select:false,
            },
            {
                name:'谈判',
                value:4,
                select:false,
            },
            {
                name:'交付',
                value:6,
                select:false,
            },
        ];
        $scope.statusList=[
            {
                name:'开放',
                value:0,
                select:false,
            },
            {
                name:'赢得',
                value:1,
                select:false,
            },
            {
                name:'失败',
                value:2,
                select:false,
            },
            {
                name:'没有机会',
                value:3,
                select:false,
            },
            {
                name:'取消',
                value:4,
                select:false,
            },
            {
                name:'关闭',
                value:5,
                select:false,
            },
        ];
        $scope.getState=function (num) {
            return $scope.stateList[num].name;
        }
        $scope.getStatus=function (num) {
            return $scope.statusList[num].name;
        }

        //分页控制实现
        $scope.pageSize=10;
        $scope.initPageSort=function(item){
            $scope.data = item;
            $scope.dataNum=item.length;
            $scope.pages = Math.ceil($scope.data.length / $scope.pageSize); //分页数
            $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
            $scope.pageList = [];
            $scope.selPage = 1;
            //设置表格数据源(分页)
            $scope.setData = function () {
                $scope.items = $scope.data.slice(($scope.pageSize * ($scope.selPage - 1)), ($scope.selPage * $scope.pageSize));//通过当前页数筛选出表格当前显示数据
            };
            $scope.items = $scope.data.slice(0, $scope.pageSize);
            //分页要repeat的数组
            for (var i = 0; i < $scope.newPages; i++) {
                $scope.pageList.push(i + 1);
            }
            //打印当前选中页索引
            $scope.selectPage = function (page) {
                //不能小于1大于最大
                if (page < 1 || page > $scope.pages) return;
                //最多显示分页数5
                if (page > 2) {
                    //因为只显示5个页数，大于2页开始分页转换
                    var newpageList = [];
                    for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)); i++) {
                        newpageList.push(i + 1);
                    }
                    $scope.pageList = newpageList;
                }
                $scope.selPage = page;
                $scope.setData();
                $scope.isActivePage(page);
                //console.log("选择的页：" + page);
            }
        };
        //设置当前选中页样式
        $scope.isActivePage = function (page) {
            return $scope.selPage == page;
        };
        //上一页
        $scope.Previous = function () {
            $scope.selectPage($scope.selPage - 1);
        };
        //下一页
        $scope.Next = function () {
            $scope.selectPage($scope.selPage + 1);
        };

        $scope.returnAllListLength = function(){
            return $scope.oppList.length;
        };

        // 排序
        $scope.orderByKey=function (key) {
            $scope.reverse=($scope.orderKey==key)?!$scope.reverse:false;
            $scope.orderKey=key;
        };
        $scope.orderList=function (item) {
            return item[$scope.orderKey];
        };


        $scope.$watch(function () {
            return $scope.searchInfo;
        },function () {
            if ($scope.searchInfo!=null&&$scope.searchInfo!=''){
                var newList=[];
                _.forEach($scope.oppList,function (data) {
                    if (data.name.indexOf($scope.searchInfo)>=0||data.customName.indexOf($scope.searchInfo)>=0){
                        newList.push(data)
                    }
                })
                $scope.initPageSort(newList)
            }else{
                if($scope.oppList!=null && $scope.oppList.length>0){
                    $scope.initPageSort($scope.oppList);
                }
            }
        })

        $scope.goClassify = function(){
            oppBase.get({id:$rootScope.opp.id},function (result) {
                $scope.presentInfo = result;
                $scope.goSref('myOppClassify');
            });

        };
    })